package org.androidpn.server.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.androidpn.server.dao.FriendMessageDao;
import org.androidpn.server.entity.FriendMessage;
import org.androidpn.server.entity.FriendMessage.FriendMessageStatus;
import org.androidpn.server.service.FriendMessageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * service FriendMessage
 * 
 * @author 蔡琪
 * 
 */
@Service("friendMessageService")
public class FriendMessageServiceImpl extends
		BaseServiceImpl<FriendMessage, Long> implements FriendMessageService {

	@Resource(name = "friendMessageDao")
	private FriendMessageDao friendMessageDao;

	@Resource(name = "friendMessageDao")
	public void setBaseDao(FriendMessageDao friendMessageDao) {
		super.setBaseDao(friendMessageDao);
	}

	@Override
	public List<FriendMessage> findNewFriendMessagesByFriendId(Long friendId,
			Long roleId) {
		return friendMessageDao.findNewFriendMessagesBySender(friendId,
				roleId);
	}

	@Transactional(readOnly = true)
	public List<FriendMessage> findFriendMessagesByReceiverId(Long roleId) {
		return friendMessageDao.findFriendMessagesByReceiverId(roleId);
	}

	@Transactional
	public void sendDone(List<FriendMessage> friendMessages) {
		for (FriendMessage friendMessage : friendMessages) {
			friendMessage.setFriendMessageStatus(FriendMessageStatus.send);
			friendMessageDao.merge(friendMessage);
		}
	}

}
